package com.example.javascaffold.utils;

import com.example.javascaffold.constant.Constant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * <p><strong>返回结果的打包工具</strong></p>
 * <p>(已封装进 @PageQuery 与 @InfoResult 中)</p>
 * <li>
 * <p>
 *     <strong>示例1: 带分页的返回</strong> <br>
 *     Page pageInfo = startPage(<br>
 *              &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
 *              Integer.parseInt(params.get(Constant.DEFAULT_PAGE_NAME).toString()),<br>
 *              &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
 *              Integer.parseInt(params.get(Constant.DEFAULT_LIMIT_NAME).toString())<br>
 *                );<br>
 *     return PageResultUtils.infoResult(page, resultData);
 * </p>
 * </li>
 * <li>
 * <p>
 *     <strong>示例2: 不带分页的返回</strong> <br>
 *     return PageResultUtils.infoResult(resultData);<br>
 * </p>
 * </li>
 * @author song
 *
 * @see Page
 * @see Map
 * @see HashMap
 * @see Object
 * @since 10:02 2021/7/31
**/
public class PageResultUtils {

    private PageResultUtils() {}

    /**
     * <p>
     *     <strong>示例1: 带分页的返回</strong> <br>
     *     Page pageInfo = startPage(<br>
     *              &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
     *              Integer.parseInt(params.get(Constant.DEFAULT_PAGE_NAME).toString()),<br>
     *              &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
     *              Integer.parseInt(params.get(Constant.DEFAULT_LIMIT_NAME).toString())<br>
     *                );<br>
     *     return PageResultUtils.infoResult(page, resultData);
     * </p>
     * @author song
     * @param page 分页page
     * @param resultData 传入的结果
     * @return HashMap
     * @since 2021/5/30
     */
    public static Map<String, Object> infoResult(Page<?> page, Object resultData) {
        PageInfo<?> pageInfo = new PageInfo<>(page);
        HashMap<String, Object> resultMap = new HashMap<>(Constant.DEFAULT_MAP_NUM);
        resultMap.put("page", pageInfo.getPageNum());
        resultMap.put("limit", pageInfo.getPageSize());
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("prePage", pageInfo.getPrePage());
        resultMap.put("nextPage", pageInfo.getNextPage());
        resultMap.put("result", resultData);
        return resultMap;
    }

    /**
     * <p>
     *     <strong>示例2: 不带分页的返回</strong> <br>
     *     return PageResultUtils.infoResult(resultData);<br>
     * </p>
     * @author song
     * @param  resultData 返回的结果
     * @return Map
     * @since 2021/5/30
     */
    public static Map<String, Object> infoResult(Object resultData) {
        HashMap<String, Object> resultMap = new HashMap<>(Constant.DEFAULT_MAP_NUM);
        resultMap.put("result", resultData);
        return resultMap;
    }

    /**
     * <p>
     *     <strong>示例3: 传入的是Map则直接将map的内容全部放入</strong> <br>
     *     return PageResultUtils.infoResult(resultData);<br>
     * </p>
     * @author song
     * @param  resultData 返回的结果
     * @return Map
     * @since 2021/9/17
     */
    public static Map<String, Object> infoResult(Page<?> page, Map<String, Object> resultData) {
        PageInfo<?> pageInfo = new PageInfo<>(page);
        HashMap<String, Object> resultMap = new HashMap<>(Constant.DEFAULT_MAP_NUM);
        resultMap.put("page", pageInfo.getPageNum());
        resultMap.put("limit", pageInfo.getPageSize());
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("prePage", pageInfo.getPrePage());
        resultMap.put("nextPage", pageInfo.getNextPage());
        resultMap.putAll(resultData);
        return resultMap;
    }
}
